//
// Created by hunan on 25-7-20.
//

#include "download_rate.h"

#include "public_module/date_util/data_util.h"
#include "public_module/log_module/log_public_module.h"

#include <cstdio>

void DownloadRate::AddData(int data_size)
{
    uint64_t now = DateUtil::GetTimeOfSecond();
    if (m_begin_tm > 0 && m_begin_tm == now)
    {
        m_data += data_size;
    }
    else if (m_begin_tm > 0 && (now - m_begin_tm) >= 1)
    {
        LOG_PUBLIC_MODULE("DownloadRate(%p) AddData download rate data:%0.2lfKB/s", this, m_data/1024.0/(now - m_begin_tm));
        m_data = data_size;
        m_begin_tm = now;
    }
    else
    {
        m_data = data_size;
        m_begin_tm = now;
    }
}

void DownloadRate::PrintStatistic() const
{
    LOG_PUBLIC_MODULE("DownloadRate(%p) PrintStatistic download rate data:%0.2lfKB/s", this, m_data/1024.0);
}
